package newWork.controller;

import newWork.constant.ResultConstant;
import newWork.entity.CommonResult;
import newWork.entity.StaffEntity;
import newWork.entity.User;
import newWork.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private IStaffService staffService;

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult doLogin(@RequestBody User user, HttpSession session) {
        StaffEntity staffEntity = new StaffEntity();
        // 将前端传来的账号密码赋值进入staff实体中
        staffEntity.setStaffId(Integer.parseInt(user.getUsername()));
        staffEntity.setStaffPassword(user.getPassword());
        // 对staff实体进行查询 返回的是list但是只有一个
        List<StaffEntity> staff = staffService.select(staffEntity);
        if (staff.isEmpty()) {
            // 如果为空就返回错误
            return new CommonResult(ResultConstant.LOGIN_FAIL_CODE, ResultConstant.FAIL_MSG);
        }
        //密码置空防止泄露
        staff.get(0).setStaffPassword(null);
        session.setAttribute("user", staff.get(0));
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG, staff.get(0).getStaffType());
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String doLogOut(HttpSession session) {
        session.removeAttribute("user");
        return "/login";
    }


//    @RequestMapping(value = "/staff", method = RequestMethod.GET)
//    public String staff(HttpSession session) {
//        StaffEntity staffEntity = (StaffEntity) session.getAttribute("user");
//        System.out.println(staffEntity);
//        if (staffEntity == null || (staffEntity.getStaffType() != 0 && staffEntity.getStaffType() != 1)) {
//            return "/error";
//        }
//        return "/staff";
//    }
//
//    @RequestMapping(value = "/admin", method = RequestMethod.GET)
//    public String admin(HttpSession session) {
//        StaffEntity staffEntity = (StaffEntity) session.getAttribute("user");
//        if (staffEntity == null || staffEntity.getStaffType() != 4) {
//            return "/error";
//        }
//        return "/admin";
//    }
//
//    @RequestMapping(value = "/master", method = RequestMethod.GET)
//    public String master(HttpSession session) {
//        StaffEntity staffEntity = (StaffEntity) session.getAttribute("user");
//        if (staffEntity == null || staffEntity.getStaffType() != 3) {
//            return "/error";
//        }
//        return "/master";
//    }
//
//    @RequestMapping(value = "/staff_head", method = RequestMethod.GET)
//    public String staff_head(HttpSession session) {
//        StaffEntity staffEntity = (StaffEntity) session.getAttribute("user");
//        if (staffEntity == null || staffEntity.getStaffType() != 2) {
//            return "/error";
//        }
//        return "/staff_head";
//    }
}
